//1、在需要使用该控件的js文件 中引入areaPick.js，
//2、在需要使用控件的js.data中地区选择器数据
// areaData: {
//   current: 0,
//   province: [],
//   city: [],
//   region: [],
//   town: [],
//   provinceObjects: [],
//   cityObjects: [],
//   regionObjects: [],
//   townObjects: [],
//   starAreaSelectedStr: '请选择省市区',
//   endAreaSelectedStr: '请选择省市区',
//   maskVisual: 'hidden',
//   provinceName: '请选择',
//   isDefault: true,
// },
// 3、在需要使用wxss中引入areaPick.wxss @import 'xxxxxxxx/areaPick/areaPick.wxss';

function areaPickDoor(that, e) {
    console.log('arePickTap', e);
    if (e.currentTarget.dataset.name) {
        console.log('赋值了没', e.currentTarget.dataset.name);
        that.setData({
            areaPickTag: e.currentTarget.dataset.name
        })
    }
    
    if (e.type == 'move' && e.currentTarget.dataset.stopMove) {
        return;
    }
    var mothod = e.currentTarget.dataset.mothod
    if (mothod == 'cascadePopup') {
        cascadePopup(that, e);
    } else if (mothod == 'cascadeDismiss') {
        cascadeDismiss(that, e);
    } else if (mothod == 'currentChanged') {
        currentChanged(that, e);
    } else if (mothod == 'changeCurrent') {
        changeCurrent(that, e);
    } else if (mothod == 'provinceTapped') {
        provinceTapped(that, e);
    } else if (mothod == 'cityTapped') {
        cityTapped(that, e);
    } else if (mothod == 'regionTapped') {
        regionTapped(that, e);
    }
    else if (mothod == 'areaChooseComplete') {
        areaChooseComplete(that, e);
    }

}


//弹出地区选择器
function cascadePopup(that, e) {
    console.log('地区弹出', 'that:', that, 'e:', e);
    //这个选择器因为是用动画效果强行推上来的，所有在显示的优先级上比较低，有可能被一些文字遮挡
    //这里在弹出时对她们进行隐藏，为这些被遮挡对控件谁Hidden属性
    that.setData({
		Hidden:true
	})
    var animation = wx.createAnimation({
        duration: 500,
        timingFunction: 'ease-in-out',
    });
    that.animation = animation;
    animation.translateY(-285).step();
    that.setData({
        areaData: {
            maskVisual: 'show',
            animationData: that.animation.export(),
            areaPickType: e.currentTarget.id
        }
    });
    var parameter = { Meth: 8 };
    getAreaList(parameter, that, 0);
}

//收回地区选择器
function cascadeDismiss(that, e) {
    that.setData({
		Hidden:false
	})
    that.animation.translateY(285).step();
    that.setData({
        areaData: {
            animationData: that.animation.export(),
            maskVisual: 'hidden'
        }
    });
}

//根据城市id获取子城市列表
function getAreaList(parameter, that, tag) {
    wx.showToast({
        title: '加载城市中...',
        icon: 'loading',
        duration: 10000
    });
    // metn = 
    var citys = new Array();

    wx.request({
        url: 'https://www.lanshuicar.com/Server/ByIDAddress.ashx',
        data: parameter,
        method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
        success: function (res) {
            citys = res.data.data;
            getCitysSuccess(citys, that, tag);
        },
        fail: function () {
            // fail
        },
        complete: function () {
            wx.hideToast();
        }
    })
}

//获取城市列表成功
function getCitysSuccess(citys, that, tag) {
    //进来默认加载省列表成功
    if (tag == 0) {
        //获取省成功
        var newAreaData = {
            province: citys,
            current: tag
        }
        setData(that, newAreaData);

    } else if (tag == 1) {
        var newAreaData = { regionName: '', city: citys, current: tag };
        var areaData = setData(that, newAreaData);

    } else if (tag == 2) {
        //获取县级市成功
        var newAreaData = {
            region: citys,
            current: tag
        }
        var areaData = setData(that, newAreaData);
        setData(that, newAreaData);
    }
}

function currentChanged(that, e) {
    // swiper滚动使得current值被动变化，用于高亮标记
    var current = e.detail.current;
    var data = { current: current };
    setData(that, data);
}

function changeCurrent(that, e) {
    // 记录点击的标题所在的区级级别
    var current = e.currentTarget.dataset.current;
    var data = { current: current };
    setData(that, data);
}

//选择省
function provinceTapped(that, e) {
    // 标识当前点击省份，记录其名称与主键id都依赖它
    var index = e.currentTarget.dataset.index;
    var id = that.data.areaData.province[index].aProvinceId;
    var provinceName = that.data.areaData.province[index].aProvinceName;
    // current为1，使得页面向左滑动一页至市级列表
    // provinceIndex是市区数据的标识
    var data = {
        provinceName: provinceName, //省名
        provinceIndex: index,	//省名列表选择的下标
        cityName: '请选择',	//省级市名——上方
        cityIndex: -1,
        regionIndex: -1,
        region: [],
        cityID: id,
    }
    setData(that, data);
    var parameter = { Meth: 5, ID: id }
    getAreaList(parameter, that, 1);
    pickTap(that, id, 1);

}

//选择省级市区
function cityTapped(that, e) {
    // 标识当前点击县级，记录其名称与主键id都依赖它
    var index = e.currentTarget.dataset.index;
    var cityName = that.data.areaData.city[index].aCityName;
    var id = that.data.areaData.city[index].aCityId;
    // current为1，使得页面向左滑动一页至市级列表
    // cityIndex是市区数据的标识
    var data = {
        cityIndex: index,	//省级市列表-选择下标
        regionIndex: -1,	//县级市列表为-1.什么都没选
        cityName: cityName,
        regionName: '请选择',
    };
    setData(that, data);

    var parameter = { Meth: 7, ID: id }
    getAreaList(parameter, that, 2);
    pickTap(that, id, 0);
}

//选择县级市
function regionTapped(that, e) {
    console.log('县级城市', e);
    var index = e.currentTarget.dataset.index;
    var regionName = that.data.areaData.region[index].aDistrictName;
    var id = that.data.areaData.region[index].aDistrictId;
    var areaSelectedStr = that.data.areaData.provinceName + ' ' + that.data.areaData.cityName + ' ' + regionName;
    var areaPickTag = that.data.areaPickTag;//标记 要将地点赋值给哪个文本框
    that.setData(JSON.parse('{"' + areaPickTag + '":"' + areaSelectedStr + '"}'));
    var data = { regionName: regionName };
    setData(that, data);
    //选择器消失
    cascadeDismiss(that, e);
    pickTap(that, id, 0);
    console.log(that);
}

//选择城市完成
//如果provincep为1，就说明刚刚选择地是省，那么就给省id赋值
function pickTap(that, id, provincep) {
    var name = that.data.areaPickTag;   //文本框标记
    var suffix = provincep == 1 ? 'Province' : 'City'   //标记赋值给省 还是给市
    var key = name + suffix;
    that.setData(JSON.parse('{"' + key + '":"' + id + '"}'));
    console.log('城市选择完成', that.data);
}

//地区选择器完成按钮响应事件
function areaChooseComplete(that, e) {
    if (that.data.areaData.cityIndex == -1) {
        wx.showModal({
            title: '提示',
            content: '请选至少选择一个城市',
        })
        return;
    }
    var provinceName = '';
    var cityName = '';
    var regionName = '';
    if (that.data.areaData.provinceName) {
        if (that.data.areaData.provinceName != '请选择') {
            provinceName = that.data.areaData.provinceName;
        }
    }
    if (that.data.areaData.cityName) {
        if (that.data.areaData.cityName != '请选择') {
            cityName = that.data.areaData.cityName;
        }
    }
    if (that.data.areaData.regionName) {
        if (that.data.areaData.regionName != '请选择') {
            regionName = that.data.areaData.regionName;
        }
    }
    var areaSelectedStr = provinceName + ' ' + cityName + ' ' + regionName;
    var areaPickTag = that.data.areaPickTag;
    that.setData(JSON.parse('{"' + areaPickTag + '":"' + areaSelectedStr + '"}'));
    cascadeDismiss(that, e);

}




/*************************************************************/
//合并两个json数据，并覆盖相同属性
function mergeJson(jsonbject1, jsonbject2) {
    var resultJsonObject = {};
    for (var attr in jsonbject1) {
        resultJsonObject[attr] = jsonbject1[attr];
    }
    for (var attr in jsonbject2) {
        resultJsonObject[attr] = jsonbject2[attr];
    }
    return resultJsonObject;
};

function setData(that, newAreaData) {
    var areaData = mergeJson(that.data.areaData, newAreaData);
    that.setData({
        areaData: areaData
    })
    return that.data.areaData;
}

//对外开放
module.exports.areaPickDoor = areaPickDoor;
